package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.CameraInfoVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotYsyCameraMapper;
import com.ruoyi.system.domain.IotYsyCamera;
import com.ruoyi.system.service.IIotYsyCameraService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author weikeauto
 * @date 2024-12-18
 */
@Service
public class IotYsyCameraServiceImpl implements IIotYsyCameraService 
{
    @Autowired
    private IotYsyCameraMapper iotYsyCameraMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public IotYsyCamera selectIotYsyCameraById(Long id)
    {
        return iotYsyCameraMapper.selectIotYsyCameraById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param iotYsyCamera 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<IotYsyCamera> selectIotYsyCameraList(IotYsyCamera iotYsyCamera)
    {
        return iotYsyCameraMapper.selectIotYsyCameraList(iotYsyCamera);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param iotYsyCamera 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIotYsyCamera(IotYsyCamera iotYsyCamera)
    {
        iotYsyCamera.setCreateTime(DateUtils.getNowDate());
        return iotYsyCameraMapper.insertIotYsyCamera(iotYsyCamera);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param iotYsyCamera 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIotYsyCamera(IotYsyCamera iotYsyCamera)
    {
        iotYsyCamera.setUpdateTime(DateUtils.getNowDate());
        return iotYsyCameraMapper.updateIotYsyCamera(iotYsyCamera);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIotYsyCameraByIds(Long[] ids)
    {
        return iotYsyCameraMapper.deleteIotYsyCameraByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIotYsyCameraById(Long id)
    {
        return iotYsyCameraMapper.deleteIotYsyCameraById(id);
    }

    @Override
    public String getInfoBySerial(String deviceSerial) {
//        HttpUtil.get(url);
//        HttpUtil.post();
        return null;
    }

    @Override
    public List<CameraInfoVo> getCameraByDeviceId(Long deviceId) {
        IotYsyCamera iotYsyCamera = iotYsyCameraMapper.getCameraByDeviceId(deviceId);
        if (iotYsyCamera == null) {
            throw new RuntimeException("该设备没有绑定监控！");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("accessToken", getToken());
        map.put("deviceSerial", iotYsyCamera.getDeviceSerial()); // FC7575801
        String cameraInfo = HttpUtil.post("https://open.ys7.com/api/lapp/device/camera/list",map);
        System.out.println(cameraInfo);
        JSONObject json = JSONObject.parseObject(cameraInfo);
        int code = json.getIntValue("code");
        if (code != 200) {
            throw new RuntimeException(json.getString("msg"));
        }
        List<CameraInfoVo> cameraInfoVoList = JSON.parseArray(json.getString("data"), CameraInfoVo.class);
        if(CollectionUtils.isEmpty(cameraInfoVoList)){
            throw new RuntimeException("无监控信息");
        }
        return cameraInfoVoList;
    }

    private String getToken(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appKey", "1f1d31c45b9e4282ac2e54264811330c");
        paramMap.put("appSecret", "d0c204b18af15a0f094f0ada419c44a2");
        String url="https://open.ys7.com/api/lapp/token/get";
        String result = HttpUtil.post(url,paramMap);
        System.out.println(result);
        JSONObject json = JSONObject.parseObject(result);
        int code = json.getIntValue("code");
        if (code != 200) {
            throw new RuntimeException(json.getString("msg"));
        }
        return json.getJSONObject("data").getString("accessToken");
    }

    public static void main(String[] args) {
//        HashMap<String, Object> paramMap = new HashMap<>();
//        paramMap.put("appKey", "7578c74ce37a4daf968f92e363e6048f");
//        paramMap.put("appSecret", "74a8d8d6f03ca76fe64e4b7a2131397c");
//        String url="https://open.ys7.com/api/lapp/token/get";
//        String result = HttpUtil.post(url,paramMap);
//        System.out.println(result);
//        JSONObject json = JSONObject.parseObject(result);
//        int code = json.getIntValue("code");
//        if (code != 200) {
//            throw new RuntimeException(json.getString("msg"));
//        }
//        String accessToken = json.getJSONObject("data").getString("accessToken");
//        System.out.println(accessToken);


        HashMap<String, Object> map = new HashMap<>();
        map.put("accessToken", "at.021iv1gt1qtwx9scdip8hl3y5r9gbmq2-4g4ka8dv43-1fka17l-6jahh1rs4");
        map.put("deviceSerial", "K95944456");
        String cameraInfo = HttpUtil.post("https://open.ys7.com/api/lapp/device/camera/list",map);
        System.out.println(cameraInfo);
        JSONObject json = JSONObject.parseObject(cameraInfo);
        List<CameraInfoVo> cameraInfoVoList = JSON.parseArray(json.getString("data"), CameraInfoVo.class);
        System.out.println(111);


    }

}
